package leetcode.二叉树;

import leetcode.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Leetcode515 {
    public List<Integer> largestValues(TreeNode root) {
        if(root==null){
            return new ArrayList<Integer>();
        }
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(q.isEmpty()){
            int size = q.size();
            int max = Integer.MIN_VALUE;
            while(size--!=0){
                TreeNode tmp = q.poll();
                max = Math.max(max,tmp.val);
                if(tmp.left!=null){
                    q.offer(tmp.left);
                }
                if(tmp.right!=null){
                    q.offer(tmp.right);
                }
            }
            res.add(max);
        }
        return res;
    }
}
